/***********************************
 * name:user_adc
 * function:初始化adc以及完成adc数据搬运与电压转换以及数据测量
 * use1:在adc_task函数中进行数据搬运和转换
 * use2:在adc_task函数中调用数据获取函数,获取测量数据,数据存放在wave_dat结构体中
 * time:2025.5.24
 ***********************************/
#include "user_adc.h"
#include "math.h"
#include <stdlib.h>
signal_data_t current_signal;
wave_display_t wave_display;

signal_data_t current_signal_1;
wave_display_t wave_display_1;
//两路ADC采样率设置
uint32_t SAMPLE_RATE = 25000;        // 采样频率 40kHz
uint32_t SAMPLE_RATE_1 = 500;        // 采样频率 40kHz
//DMA 数据搬运数组
uint32_t adc_dma_buffer[BUFFER_SIZE];//dma数据搬运缓冲区
uint32_t adc_dma_buffer_1[BUFFER_SIZE];//dma数据搬运缓冲区
//显示数组
uint32_t adc_disp_buffer[BUFFER_SIZE];//dma数据搬运缓冲区
uint32_t adc_disp_buffer_1[BUFFER_SIZE];//dma数据搬运缓冲区
//数据处理数组
float user_read_buffer[BUFFER_SIZE];//数据待处理缓冲区
float user_read_buffer_1[BUFFER_SIZE];//数据待处理缓冲区
//数据处理标志位
uint8_t adc_cplt_flag;//缓冲区满标志位 0 - 未满 1 - 满
uint8_t adc_cplt_flag_1;//缓冲区满标志位 0 - 未满 1 - 满
//去直流偏置数据存储数组
float ac_signal_1[BUFFER_SIZE] = {0};
float ac_signal[BUFFER_SIZE] = {0};
//ADC有效通道选择标志位
uint8_t tim_change_flag = 0;
//防跳变结构体
typedef struct {
    /* data */
    uint8_t state;
    uint32_t time[2];
    uint8_t index;
    uint32_t count;
}jump_t;
jump_t jump_check;
//信号数据存储结构体
wave_t wave_adc;
wave_t wave_adc_1;
void wave_init_user(void)
{
    current_signal.sample_data = adc_disp_buffer;
    current_signal.sample_rate = 25000.0f; // 20kHz采样率

    current_signal_1.sample_data = adc_disp_buffer_1;
    current_signal_1.sample_rate = 500.0f; // 20kHz采样率
    wave_display_init(&wave_display, &u8g2, &current_signal);
    wave_display_init(&wave_display_1, &u8g2, &current_signal_1);
}
void user_display_run(uint8_t tim_change_flag)
{
    switch(tim_change_flag)
    {
        case 0://高频通道
            current_signal.frequency = wave_adc.wave_freq;
            current_signal.peak_to_peak = wave_adc.wave_peak;
            current_signal.main_freq = wave_adc.wave_freq_f;
            current_signal.main_amplitude = wave_adc.wave_amplitude;
            current_signal.sample_count = 1024;
            // 更新显示
            wave_display_update(&wave_display);
        break;

        case 1://低频通道
            current_signal_1.frequency = wave_adc_1.wave_freq;
            current_signal_1.peak_to_peak = wave_adc_1.wave_peak;
            current_signal_1.main_freq = wave_adc_1.wave_freq_f;
            current_signal_1.main_amplitude = wave_adc_1.wave_amplitude;
            current_signal_1.sample_count = 1024;
            // 更新显示
            wave_display_update(&wave_display_1);
        break;
    }
}
/**
 * @brief adc采样初始化
 * @param none
 * @return none
 */
void user_adc_init(void)
{

    //ADC采样DMA模式开启
    HAL_ADC_Start_DMA(&hadc1,adc_dma_buffer,BUFFER_SIZE);
    //关闭DMA半满中断
    __HAL_DMA_DISABLE_IT(&hdma_adc1,DMA_IT_HT);
     HAL_TIM_Base_Start(&htim2);

    HAL_ADC_Start_DMA(&hadc2,adc_dma_buffer_1,BUFFER_SIZE);
    //关闭DMA半满中断
    __HAL_DMA_DISABLE_IT(&hdma_adc2,DMA_IT_HT);
    //开启ADC触发定时器
     HAL_TIM_Base_Start(&htim3);
    
}
/**
 * @brief 指定数量数据转换完成回调函数
 * @param hadc
 * @return none
 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
    //判断是否是目标ADC
    if(hadc->Instance == ADC1)
    {
        //停止dma搬运
        HAL_ADC_Stop_DMA(hadc);
        //拉高完成标志位，通知cpu已经完成
        adc_cplt_flag = 1;
    }
    if(hadc->Instance == ADC2)
    {
        HAL_ADC_Stop_DMA(hadc);
        adc_cplt_flag_1 = 1;
    }
}
/**
 * @brief adc数据处理函数
 * @param none
 * @return none
 */
void adc_task(void)
{
    //判断是否可以进行任务处理
    if(adc_cplt_flag == 1)
    {
        //user_printf(&huart1,"OK\r\n");
        /*数据搬运操作*/
        for(uint16_t i = 0 ;i < BUFFER_SIZE;i++)
        {
            //取出dma缓冲区的数据
            user_read_buffer[i] = ((float)adc_dma_buffer[i] * 3.3f *3.0f / 4095.0f);
            adc_disp_buffer[i] = adc_dma_buffer[i];
            //user_printf(&huart1,"user_read_buffer:%.3f\r\n",user_read_buffer[i]);
        }
        // 计算信号的平均值（直流分量）
        float dc_offset = 0.0f;//定义信号平均值存储变量
        for (int i = 0; i < BUFFER_SIZE; i++) 
        {
            //buffer数据求和
            dc_offset += user_read_buffer[i];
        }
        //求出buffer平均值即直流分量
        dc_offset /= BUFFER_SIZE;
        //去除直流分量
        //创建新数组用以存储去除直流分量后的数据
        for (int i = 0; i < BUFFER_SIZE; i++) 
        {
            //数据存储,用于后续数据处理
            ac_signal[i] = user_read_buffer[i] - dc_offset;
        }
        /*数据处理操作 - 高频*/
        get_wave_dat_t(&wave_adc,ac_signal,SAMPLE_RATE);
        get_wave_dat_f(&wave_adc,ac_signal,fft_input,fft_output,SAMPLE_RATE);

        //user_printf(&huart1,"peak:%.3f,freq:%.3f\r\n",wave_adc.wave_peak,wave_adc.wave_freq);
        /*current_signal.frequency = wave_adc.wave_freq;
        current_signal.peak_to_peak = wave_adc.wave_peak;
        current_signal.main_freq = wave_adc.wave_freq_f;
        current_signal.main_amplitude = wave_adc.wave_amplitude;
        current_signal.sample_count = 1024;
    // 更新显示
        wave_display_update(&wave_display);*/

        /*状态清空操作*/
        //清空dma缓冲区
        memset(adc_dma_buffer,0,sizeof(uint32_t) * BUFFER_SIZE);
        //拉低标志位等待下次数据处理
        adc_cplt_flag = 0;
        //重新开启
        HAL_ADC_Start_DMA(&hadc1,adc_dma_buffer,BUFFER_SIZE);
        //关闭DMA半满中断
        __HAL_DMA_DISABLE_IT(&hdma_adc1,DMA_IT_HT);
    }
    //低频触发段
    if(adc_cplt_flag_1 == 1)
    {
         //user_printf(&huart1,"OK\r\n");
        /*数据搬运操作*/
        for(uint16_t i = 0 ;i < BUFFER_SIZE;i++)
        {
            //取出dma缓冲区的数据
            user_read_buffer_1[i] = ((float)adc_dma_buffer_1[i] * 3.3f * 3.0f / 4095.0f);
            adc_disp_buffer_1[i] = adc_dma_buffer_1[i];
            //user_printf(&huart1,"user_read_buffer:%.3f\r\n",user_read_buffer[i]);
        }
        // 计算信号的平均值（直流分量）
        float dc_offset_1 = 0.0f;//定义信号平均值存储变量
        for (int i = 0; i < BUFFER_SIZE; i++) 
        {
            //buffer数据求和
            dc_offset_1+= user_read_buffer_1[i];
        }
        //求出buffer平均值即直流分量
        dc_offset_1/=BUFFER_SIZE;
        //去除直流分量
        //创建新数组用以存储去除直流分量后的数据
        for (int i = 0; i < BUFFER_SIZE; i++) 
        {
            //数据存储,用于后续数据处理
            ac_signal_1[i] = user_read_buffer_1[i] - dc_offset_1;
        }
        get_wave_dat_t(&wave_adc_1,ac_signal_1,SAMPLE_RATE_1);
        get_wave_dat_f(&wave_adc_1,ac_signal_1,fft_input_1,fft_output_1,SAMPLE_RATE_1);
        //wave_adc_1.wave_freq = wave_adc_1.wave_freq_f;
        //user_printf(&huart1,"peak:%.3f,freq:%.3f\r\n",wave_adc.wave_peak,wave_adc.wave_freq);

        /*状态清空操作*/
        //清空dma缓冲区
        memset(adc_dma_buffer_1,0,sizeof(uint32_t) * BUFFER_SIZE);
        //拉低标志位等待下次数据处理
        adc_cplt_flag_1 = 0;

        HAL_ADC_Start_DMA(&hadc2,adc_dma_buffer_1,BUFFER_SIZE);
        //关闭DMA半满中断
        __HAL_DMA_DISABLE_IT(&hdma_adc2,DMA_IT_HT);
    }
    if((wave_adc.wave_freq_f < 100.0f &&wave_adc.wave_freq_f >= 0.1)&&tim_change_flag == 0)
    {
        //拉高低频标志位，更改采样率
        tim_change_flag = 1;
        jump_check.count = 0;
        //清空缓冲区确保数据无误
        memset(ac_signal_1,0,BUFFER_SIZE*sizeof(uint32_t));
    }
    if(wave_adc.wave_freq_f >100.0f  && wave_adc.wave_freq_f < 10000 && tim_change_flag == 1)
    {

        if(jump_check.count == 0)
        {
            jump_check.count = 1;
        }    
        if(jump_check.count != 0)
        {
            jump_check.count++;
        }
        if(jump_check.count > 1000)
        {
            jump_check.count = 0;
            tim_change_flag = 0;
        }
    }
    //user_disp_func((tim_change_flag == 0)?wave_adc:wave_adc_1);

    
    // 获取你的信号测量数据
        
    // 更新显示
    //signal_display_update(&u8g2, (tim_change_flag == 0)? &wave_adc : &wave_adc_1);
    //signal_display_spectrum(&u8g2, (tim_change_flag == 0)? &wave_adc : &wave_adc_1);  

    //signal_display_spectrum(&u8g2,(tim_change_flag == 0)? &wave_adc : &wave_adc_1,(tim_change_flag == 0)?fft_output:fft_output_1,BUFFER_SIZE,(tim_change_flag == 0)? SAMPLE_RATE : SAMPLE_RATE_1);  
    //if(user_switch == 1)
    //signal_display_update(&u8g2, (tim_change_flag == 0)? &wave_adc : &wave_adc_1);

}

void user_display_function(void)
{
    if(user_switch == 0)
    user_display_run(tim_change_flag);
    if(user_switch == 1)
    signal_display_spectrum_test(&u8g2,(tim_change_flag == 0)? &wave_adc : &wave_adc_1,(tim_change_flag == 0)?fft_output:fft_output_1,BUFFER_SIZE,(tim_change_flag == 0)? SAMPLE_RATE : SAMPLE_RATE_1,(tim_change_flag == 0)? wave_adc.wave_peak : wave_adc_1.wave_peak);
}